﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceApplication.Service.Basic;
using CommonLibrary.DataModel.Basic;
using OAclient.Models;
using System.Workflow.Activities;
using System.Web.Security;
using OAclient.Common;
using OAclient.Filters;

namespace OAclient.Controllers
{

    [ExceptionFilter]
    [LoggerFilter]
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        BasicService basicService = new BasicService();
        [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
        public ActionResult LogOn()
        {
            return View();
        }

        public ActionResult GetPassword(String login_id)
        {
            var password = "";
            if (Request.Cookies[login_id] != null)
                password = Request.Cookies[login_id].Value;
            return Content(password);
        }

        public ActionResult GetValidateImage()
        {
            string code = ValidateCode.CreateValidateCode(4);
            Session["validate_code"] = code;
            byte[] bytes = ValidateCode.CreateValidateGraphic(code);
            return File(bytes, @"image/gif");
        }

        [HttpPost]
        [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
        public ActionResult LogOn(UserModel userModel, string user_loginid, string user_psw, string chk_remember)
        {

            try
            {
                var validate_input = Request.Form["validate_input"].ToString();
                var validate_code = Session["validate_code"].ToString();


                if (string.IsNullOrEmpty(user_loginid))
                {
                    ModelState.AddModelError("user_loginid", "请填写账号名！");
                }
                else if (string.IsNullOrEmpty(user_psw))
                {
                    ModelState.AddModelError("user_psw", "密码不能为空!");
                }
                else
                {
                    UserEntity userEntity = basicService.QueryUserByLoginid(user_loginid);
                    if (userEntity == null)
                    {
                        ModelState.AddModelError("user_loginid", "不存在该账户!");
                    }
                    else if (userEntity.user_psw != user_psw)
                    {
                        ModelState.AddModelError("user_psw", "密码错误!");
                    }
                    else
                    {
                        if (validate_code != validate_input)
                        {
                            ViewData["loginresult"] = "请重新输入验证码";
                            return View();
                        }
                        else
                        {
                            userModel.user = userEntity;
                            FormsAuthentication.SetAuthCookie(userEntity.user_name, false);//是否记住
                            if (chk_remember != "false")
                                Response.Cookies.Add(new HttpCookie(user_loginid, user_psw));
                            else
                            {
                                var hc = Request.Cookies[user_loginid];
                                if (hc != null)
                                {
                                    hc.Expires = DateTime.Now.AddDays(-1);
                                    Response.AppendCookie(hc);
                                }

                            }

                            SetConfig();
                            //if (loginid == "admin")
                            //    Response.Write("<script language='javascript'>document.location='/Manager/Main'; </script>");
                            //else
                            return RedirectToAction("Main", "Home");

                            //var navigatorType = Request.Form["navigator_type"];
                            //var newWind = "window.showModalDialog('/Home/Main','','dialogHeight:700px;dialogWidth:1500px;center:yes;resizable:no;');";
                            //Response.Write("<script language='javascript'>" + newWind + "</script>");
                        }
                    }
                }
            }
            catch
            {
                ViewData["loginresult"] = "登陆失败";
            }
            return View();
        }

        public ActionResult LogOff(UserModel userModel)
        {
            FormsAuthentication.SignOut();
            Session["_usermodel"] = null;
            return null;
        }

        public ActionResult Test()
        {
            return View();
        }

        private void SetConfig()
        {
            var configs = basicService.GetConfigs();

            //ComVariable.EmailProvider = configs["EmailProvider"];
            ComVariable.EmailReaded = configs["EmailReaded"];
            ComVariable.EmailLastDay = configs["EmailLastDay"];
        }
    }
}
